import { Action, ActionKey } from "@/action/Action";
// import { AuthorizedFeatures, Features } from "@/util/AuthorizedFeatures";
import { modelService, projectService } from "../../service";
import { localFile } from "@/localFile/LocalFile";
import { loading } from "@/common/Loading";
import { locale } from "@/util/locale";
import { ApiCode } from "@/constants/api";

export class ExportCADAction extends Action {
  keyName = ActionKey.exportCAD

  async run(modelId: string) {
    if (!modelId || !app.activeProject) return;

    // 校验是否存在封装模型
    try {
      await modelService.validExistEncryptModel({ projectId: app.activeProject.id });
    } catch (error) {
      console.error(error);
      throw new Error("项目存在封装模型,校验失败");
    }

    const res = await localFile.chooseSaveFilePath({
      title: '选择保存位置',
      filters: [{
        name: "step",
        extensions: ['step'],
      }]
    });
    const filePath = res.filePath;
    if (res.canceled || !filePath) return;

    // const filePath = 'd:\\z.step';

    const project = app.activeProject;
    // console.log(project, '>>> project');
    if (!project) throw new Error("当前无打开项目！");
    console.log("导出cad step格式");
    loading.popup(locale.t('正在导出 CAD 格式...'));
    const rootNode = await modelService.getRootNode(app.activeProject?.id || '');
    const result = await projectService.exportCAD({
      projectId: project.id,
      md3FileURL: project.config.fullPath,
      outFileURL: filePath,
      instanceIds: [rootNode.id],
      isExportAssociation: false
    });
    if (result.code === ApiCode.SUCCESS) {
      app.$notify({
        type: 'success',
        title: '成功',
        message: '保存成功'
      });
    }
    loading.close();
  }
  onSysMenuClick() {
    const project = app.activeProject;
    if (!project) return;
    this.run(project.nodePool.rootNodes[0].id);
  }
}